﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ChineseSchool.BLL;
using ChineseSchool.Models.Hethong;

namespace ChineseSchool.UserControl.ThuNghiem
{
    public partial class ActivitiesList : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadActivitiesList(1);
            }
        }

        public void LoadActivitiesList(int pageIndex)
        {
            var lstActivities = new B_Activities().GetAllWithPaging(pageIndex, 10);

            // Bind data
            rptActivities.DataSource = lstActivities.Items;
            rptActivities.DataBind();

            // Bind Pager
            GeneratePager(pageIndex, Convert.ToInt32(lstActivities.TotalPages));
        }

        public void GeneratePager(int pageIndex, int totalPage)
        {
            List<ListItem> pageLinkContainer = new List<ListItem>();

            for (int i = 1; i <= totalPage; i++)
            {
                pageLinkContainer.Add(new ListItem(i.ToString(), i.ToString(), pageIndex != i));
            }

            rptPager.DataSource = pageLinkContainer;
            rptPager.DataBind();
        }


        protected void RptActivitiesItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            var lnkDetail = (HyperLink)e.Item.FindControl("lnkActivitiDetails");

            var id = ((M_Activities)e.Item.DataItem).ActivitiesID;
            var title = ((M_Activities)e.Item.DataItem).ActivitiesTitle;

            lnkDetail.NavigateUrl = Util.MakeLinkActivitiesDetail(id.ToString(CultureInfo.InvariantCulture), Encode.Uni2KD(title.ToString(CultureInfo.InvariantCulture)));
        }

        protected void RptPagerItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "PageNo")
            {
                LoadActivitiesList(Convert.ToInt32(e.CommandArgument));
            }
        }
    }
}